﻿Imports System.Xml
Imports System.Data
Imports System.Web
Imports XMLServiceiApi

Public Class FormMain

    Dim _ds1 As DataSet
    Dim _dtColumnDefinitions As DataTable
    Dim _iColumnCount As Integer
    Dim _iRowCount As Integer
    Dim _dtReturnData As DataTable
    'Use unique IPC info for this job so it has its own
    'Dim _sIPC As String = "/tmp/xmlservicei" & Format(Now, "yyyyMMddhhmmssfff") 'Use for production for unique IPC sessions
    Dim _sIPC As String = "/tmp/xmlservicei" 'use for testing
    Dim _XML As New XmlServicei

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Try

            'Set up Data Grid Display Properties
            With DataGridView1
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .AlternatingRowsDefaultCellStyle.BackColor = Color.LightSteelBlue
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
                .MultiSelect = False
                .RowHeadersWidth = 25
                .RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing
            End With


        Catch ex As Exception

        End Try

    End Sub


    Private Sub ButtonCLCommand_Click(sender As Object, e As EventArgs) Handles ButtonCLCommand.Click

        Dim rtnbool As Boolean

        Try

            ButtonCLCommand.Enabled = False

            'Clear response
            txtResponse.Text = ""

            'Set URL and User Info for Service Call
            _XML.SetBaseURL(My.Settings.URL)
            _XML.SetUserInfo(My.Settings.User, My.Settings.Password)
            'Set unique IPC info for this job
            _XML.SetIpcInfo(_sIPC)
            'Set HTTP default timeout
            _XML.SetHttpTimeout(My.Settings.HTTPTimeout)

            'Execute the CL command
            rtnbool = _XML.ExecuteCommand(txtCLCommand.Text.Trim)

            If rtnbool = True Then
                txtResponse.Text = "Command Was Successful. XML Response: " & vbCrLf & _XML.GetLastXmlResponse
            Else
                txtResponse.Text = "Command Failed. XML Response: " & vbCrLf & _XML.GetLastXmlResponse
            End If

        Catch ex As Exception
            txtResponse.Text = "Error occurred running CL command. Error: " & ex.Message
        Finally
            ButtonCLCommand.Enabled = True
        End Try

    End Sub

    Private Sub ButtonSQLQuery_Click(sender As Object, e As EventArgs) Handles ButtonSQLQuery.Click

        Dim rtnbool As Boolean
        Dim sXMLTemp As String = "C:\rjstemp\xmlservicesqlresults" & Format(Now, "yyyyMMddhhmmssfffff") & ".xml"

        Try

            ButtonSQLQuery.Enabled = False

            'Clear response
            txtResponse.Text = ""

            'Set URL and User Info for Service Call
            _XML.SetBaseURL(My.Settings.URL)
            _XML.SetUserInfo(My.Settings.User, My.Settings.Password)
            'Set unique IPC info for this job
            _XML.SetIpcInfo(_sIPC)
            'Set HTTP default timeout
            _XML.SetHttpTimeout(My.Settings.HTTPTimeout)


            'Run SQL query to XML temp file
            rtnbool = _XML.ExecuteSqlQueryPrepared(txtSQLQuery.Text, sXMLTemp)

            If rtnbool = True Then
                txtResponse.Text = "Query Was Successful. XML Response: " & vbCrLf & _XML.GetLastXmlResponse
            Else
                txtResponse.Text = "Query Failed. XML Response: " & vbCrLf & _XML.GetLastXmlResponse
            End If

            'Load temp file results to data set
            rtnbool = _XML.LoadDataSetFromXmlFile(sXMLTemp)

            'get data table and display in grid
            DataGridView1.DataSource = _XML.GetDataTable

            'Attempt to kill temp XML results file
            Try
                System.IO.File.Delete(sXMLTemp)
            Catch ex As Exception
            End Try

        Catch ex As Exception
            txtResponse.Text = "Error occurred running CL command. Error: " & ex.Message
        Finally
            ButtonSQLQuery.Enabled = True
        End Try

    End Sub

    Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
        Try
            End
        Catch ex As Exception
        End Try
    End Sub

    Private Sub ButtonProgram_Click(sender As Object, e As EventArgs) Handles ButtonProgram.Click
        Dim rtnbool As Boolean

        Try

            ButtonProgram.Enabled = False

            'Clear response
            txtResponse.Text = ""

            'Set URL and User Info for Service Call
            _XML.SetBaseURL(My.Settings.URL)
            _XML.SetUserInfo(My.Settings.User, My.Settings.Password)
            'Set unique IPC info for this job
            _XML.SetIpcInfo(_sIPC)
            'Set HTTP default timeout
            _XML.SetHttpTimeout(My.Settings.HTTPTimeout)

            'Execute the program with parms
            Dim arrParms As New ArrayList
            Dim p1 As New XmlServicei.PgmParmList
            Dim p2 As New XmlServicei.PgmParmList

            p1.parmtype = "50A"
            p1.parmvalue = ""
            p2.parmtype = "15p2"
            p2.parmvalue = "100.00"
            arrParms.Add(p1)
            arrParms.Add(p2)

            rtnbool = _XML.ExecuteProgram("TESTCL1", "RJSDOTNET", arrParms)

            If rtnbool = True Then
                txtResponse.Text = "Command Was Successful. XML Response: " & vbCrLf & _XML.GetLastXmlResponse
            Else
                txtResponse.Text = "Command Failed. XML Response: " & vbCrLf & _XML.GetLastXmlResponse
            End If

            Dim ds1 As New DataSet
            _XML.LoadPgmCallDataSetFromXmlString(_XML.GetLastXmlResponse)

        Catch ex As Exception
            txtResponse.Text = "Error occurred running CL command. Error: " & ex.Message
        Finally
            ButtonProgram.Enabled = True
        End Try


    End Sub

    Private Sub ButtonProcedure_Click(sender As Object, e As EventArgs) Handles ButtonProcedure.Click

        Dim rtnbool As Boolean

        Try

            ButtonProcedure.Enabled = False

            'Clear response
            txtResponse.Text = ""

            'Set URL and User Info for Service Call
            _XML.SetBaseURL(My.Settings.URL)
            _XML.SetUserInfo(My.Settings.User, My.Settings.Password)
            'Set unique IPC info for this job
            _XML.SetIpcInfo(_sIPC)
            'Set HTTP default timeout
            _XML.SetHttpTimeout(My.Settings.HTTPTimeout)

            'Execute the program with parms
            Dim arrParms As New ArrayList
            Dim p1 As New XmlServicei.ProcedureParmList
            Dim rtnParms As New ArrayList
            Dim p1rtn As New XmlServicei.ProcedureParmList

            p1.parmtype = "200A"
            p1.parmvalue = "TEST12"
            p1.parmname = "argIn"
            arrParms.Add(p1)

            'Build return parms
            p1rtn.parmtype = "200A"
            p1rtn.parmvalue = ""
            p1rtn.parmname = "argIn"
            rtnParms.Add(p1rtn)

            'Execute program call 
            rtnbool = _XML.ExecuteProgramProcedure("XMLSVC001", "RJSDOTNET", "SAMPLE1", arrParms, rtnParms)

            'Handle responses
            If rtnbool = True Then
                txtResponse.Text = "Subprocedure call was successful. XML Response: " & vbCrLf & _XML.GetLastXmlResponse
            Else
                txtResponse.Text = "Subprocedure failed. XML Response: " & vbCrLf & _XML.GetLastXmlResponse
            End If

        Catch ex As Exception
            txtResponse.Text = "Error occurred running subprocedure. Error: " & ex.Message
        Finally
            ButtonProcedure.Enabled = True
        End Try


    End Sub


    Private Sub ButtonKillService_Click(sender As Object, e As EventArgs) Handles ButtonKillService.Click

        Dim rtnbool As Boolean

        Try

            ButtonKillService.Enabled = False

            'Clear response
            txtResponse.Text = ""

            'Set URL and User Info for Service Call
            _XML.SetBaseURL(My.Settings.URL)
            _XML.SetUserInfo(My.Settings.User, My.Settings.Password)
            'Set unique IPC info for this job
            _XML.SetIpcInfo(_sIPC)
            'Set HTTP default timeout
            _XML.SetHttpTimeout(My.Settings.HTTPTimeout)

            'Kill currently active XMLSERVICE job named XTOOLKIT
            rtnbool = _XML.KillService()

            'Handle responses
            If rtnbool = True Then
                txtResponse.Text = "Service shutdown was successful. XML Response: " & vbCrLf & _XML.GetLastXmlResponse
            Else
                txtResponse.Text = "Service shutdown failed. XML Response: " & vbCrLf & _XML.GetLastXmlResponse
            End If

        Catch ex As Exception
            txtResponse.Text = "Error occurred while shutting down the XMLSERVICE job: XTOOLKIT. Error: " & ex.Message
        Finally
            ButtonKillService.Enabled = True
        End Try

    End Sub

    Private Sub ButtonSQLNonQuery_Click(sender As Object, e As EventArgs) Handles ButtonSQLNonQuery.Click

        Dim sXMLTemp As String = "C:\rjstemp\xmlservicesqlresults" & Format(Now, "yyyyMMddhhmmssfffff") & ".xml"
        Dim rtnbool As Boolean

        Try

            ButtonSQLNonQuery.Enabled = False

            'Clear response
            txtResponse.Text = ""

            'Set URL and User Info for Service Call
            _XML.SetBaseURL(My.Settings.URL)
            _XML.SetUserInfo(My.Settings.User, My.Settings.Password)
            'Set unique IPC info for this job
            _XML.SetIpcInfo(_sIPC)
            'Set HTTP default timeout
            _XML.SetHttpTimeout(My.Settings.HTTPTimeout)

            'Run SQL non query to XML temp file
            rtnbool = _XML.ExecuteSqlNonQuery(txtSqlNonQuery.Text, sXMLTemp)

            If rtnbool = True Then
                txtResponse.Text = "SQL Action Was Successful. XML Response: " & vbCrLf & _XML.GetLastXmlResponse
            Else
                txtResponse.Text = "SQL Action Failed. XML Response: " & vbCrLf & _XML.GetLastXmlResponse
            End If

            'Load temp file results to data set
            rtnbool = _XML.LoadDataSetFromXmlFile(sXMLTemp)

            'get data table and display in grid
            DataGridView1.DataSource = _XML.GetDataTable

            'Attempt to kill temp XML results file
            Try
                System.IO.File.Delete(sXMLTemp)
            Catch ex As Exception
            End Try

        Catch ex As Exception
            txtResponse.Text = "Error occurred running SQL non query. Error: " & ex.Message
        Finally
            ButtonSQLNonQuery.Enabled = True
        End Try

    End Sub

    Private Sub ButtonKillIpcTemp_Click(sender As Object, e As EventArgs) Handles ButtonKillIpcTemp.Click
        Dim rtnbool As Boolean

        Try

            ButtonKillIpcTemp.Enabled = False

            'Clear response
            txtResponse.Text = ""

            'Set URL and User Info for Service Call
            _XML.SetBaseURL(My.Settings.URL)
            _XML.SetUserInfo(My.Settings.User, My.Settings.Password)
            'Set unique IPC info for this job
            _XML.SetIpcInfo(_sIPC)
            'Set HTTP default timeout
            _XML.SetHttpTimeout(My.Settings.HTTPTimeout)

            'Kill currently active XMLSERVICE job named XTOOLKIT
            rtnbool = _XML.KillIpcIfsDirectories("/tmp/xmlservicei*")

            'Handle responses
            If rtnbool = True Then
                txtResponse.Text = "Kill IPC IFS dirs was successful. XML Response: " & vbCrLf & _XML.GetLastXmlResponse
            Else
                txtResponse.Text = "Kill IPC IFS dirs failed. XML Response: " & vbCrLf & _XML.GetLastXmlResponse
            End If

        Catch ex As Exception
            txtResponse.Text = "Error occurred while killing IPC IFS dirs. Error: " & ex.Message
        Finally
            ButtonKillIpcTemp.Enabled = True
        End Try

    End Sub
End Class
